package edu.bw.web.user;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.InsertUserForm;
import edu.bw.service.UserService;
import edu.bw.service.impl.UserServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/user/insert")
public class InsertServlet extends HttpServlet {
    UserService userService =new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //{userId: 0, username: "ceshi", password: "123456", name: "测试", sex: "男", tel: "17858925679",…}

        String reqJSON = request.getReader().readLine();
        InsertUserForm insertUserForm = JSONUtil.toBean(reqJSON, InsertUserForm.class);

        Integer rows=userService.insertUser(insertUserForm);

        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows",rows)));


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
